package com.ljs.net
{
	import com.ljs.net.login.CSLoginPacket;
	import com.ljs.net.opcodes.Opcodes;
	
	import flash.utils.getDefinitionByName;

	public class PacketFactory
	{
		private static var _instance:PacketFactory; 
		private var _packetBuilder:Dictionary;
		public function PacketFactory()
		{
			if(_instance==null)
			{
				_instance=this;
			}
			_packetBuilder = new Dictionary;
		}
		
		public static function getInstance():PacketFactory
		{
			if(_instance==null)
			{
				_instance=new PacketFactory();
			}
			
			return _instance;
		}
		
		
		private function init()
		{
			_packetBuilder[Opcodes.LOGIN]="com.ljs.net.login.CSLoginPacket";
		}
		
		public function getPacketById(id:int):Packet
		{
			if(_packetBuilder[id])
			{
				var cls:class=getDefinitionByName(_packetBuilder[id]) as Class;
				var p:Packet=new cls();
				
				return p;
			}
			
			return null;
		}
	}
}